package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.dto.NursingTaskPageDto;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.mapper.NursingTaskMapper;
import com.zzyl.nursing.service.INursingTaskService;
import com.zzyl.nursing.vo.NursingTaskPageVo;
import com.zzyl.nursing.vo.QueryNursingTaskDetailVo;
import com.zzyl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * 护理任务Service业务层处理
 * @Author Ls
 * @author nmy
 * @date 2025-10-29
 */
@Service
public class NursingTaskServiceImpl extends ServiceImpl<NursingTaskMapper, NursingTask> implements INursingTaskService {

    @Autowired
    private NursingTaskMapper nursingTaskMapper;

    @Autowired
    private ElderMapper elderMapper;

    /**
     * 取消护理任务
     *
     * @param reason 取消原因
     * @param taskId 护理任务ID
     */
    @Override
    public void cancel(String reason, Long taskId) {
        //创建护理任务实体类，并将要更新的数据填充的实体类中
        NursingTask nursingTask = NursingTask.builder()
                .cancelReason(reason)
                .status(Constants.NURSING_TASK_STATUS_CANCELLED)
                .id(taskId)
                .build();
        //执行更新操作，对数据库数据进行更新
        this.updateById(nursingTask);

    }

    /**
     * 查询护理任务
     *
     * @param id 护理任务主键
     * @return 护理任务
     */
    @Override
    public QueryNursingTaskDetailVo selectNursingTaskById(Long id)
    {
        //获取Vo基本信息表
        QueryNursingTaskDetailVo vo = nursingTaskMapper.selectNursingTaskById(id);
        //填充护理员姓名
        List<String> nursingName = getNursingName(vo.getNursingId());
        Elder elder = elderMapper.selectOne(Wrappers.<Elder>lambdaQuery().eq(Elder::getId, vo.getElderId()));
        //根据老人身份证获取年龄
        int age = IdcardUtil.getAgeByIdCard(elder.getIdCardNo());
        vo.setAge(age);
        vo.setNursingName(nursingName);
        return vo;
    }

    //根据护理员id获取护理员姓名
    private List<String> getNursingName(String ids) {
        String[] nurseId =(ids.split(","));
        List<Integer> integerList = Arrays.stream(nurseId).map(Integer::parseInt).collect(Collectors.toList());
        List<SysUser> sysUserList = sysUserService.lambdaQuery().in(SysUser::getUserId, integerList).select(SysUser::getNickName).list();

        List<String> nursingName = sysUserList.stream().map(y -> {
            String nickName = y.getNickName();
            return nickName;
        }).collect(Collectors.toList());
        return nursingName;
    }
    @Autowired
    private ISysUserService sysUserService;

    /**
     *
     * @param estimatedServerTime 执行时间
     * @param mark 执行记录
     * @param taskId 护理任务ID
     * @param taskImage 任务图片
     * 查询护理任务列表
     *
     */
    @Override
    public void doNursingTask(LocalDateTime estimatedServerTime, String mark, Long taskId, String taskImage) {
        //创建护理任务实体类，并将要更新的数据填充的实体类中
        NursingTask task = NursingTask.builder()
                .id(taskId)
                .mark(mark)
                .taskImage(taskImage)
                .status(Constants.NURSING_TASK_STATUS_EXECUTED)
                .realServerTime(estimatedServerTime)
                .build();
        //执行更新操作，对数据库数据进行更新
        this.updateById(task);
    }

    /**
     * 查询护理任务列表
     * @param dto 护理任务
     * @return 护理任务
     * */
    @Override
    public Page<NursingTaskPageVo>  selectNursingTaskList(NursingTaskPageDto dto)
    {
        //求分页对象
        Page<NursingTask> page = this.lambdaQuery()
                .like(StrUtil.isNotBlank(dto.getElderName()), NursingTask::getElderName, dto.getElderName())
                .like(dto.getNurseId() != null, NursingTask::getNursingId, dto.getNurseId())
                .eq(dto.getProjectId() != null, NursingTask::getProjectId, dto.getProjectId())
                .eq(dto.getStatus() != null, NursingTask::getStatus, dto.getStatus())
                .between((dto.getStartTime()!= null && dto.getEndTime() != null), NursingTask::getEstimatedServerTime, dto.getStartTime(), dto.getEndTime())
                .page(Page.of(dto.getPageNum(), dto.getPageSize()));
        //将Page<NursingTask>转化为Page<NursingTaskPageVo>
        Page<NursingTaskPageVo> nursingTaskPageVoPage = new Page<>();
//        nursingTaskPageVoPage.setCurrent(page.getCurrent());
//        nursingTaskPageVoPage.setSize(page.getSize());
        nursingTaskPageVoPage.setTotal(page.getTotal());
        nursingTaskPageVoPage.setRecords(page.getRecords().stream().map(x -> {
            NursingTaskPageVo nursingTaskPageVo = BeanUtil.toBean(x, NursingTaskPageVo.class);
            //填充护理员姓名
            List<String> nursingName = getNursingName(x.getNursingId());
            nursingTaskPageVo.setNursingName(nursingName);
            return nursingTaskPageVo;
        }).collect(Collectors.toList()));
        //返回Page对象
        return nursingTaskPageVoPage;
    }



    /**
     * 任务改期
     *
     * @param estimatedServerTime 预计服务时间
     * @param taskId              服务任务ID
     */
    @Override
    public void updateTime(LocalDateTime estimatedServerTime, Long taskId) {
        //创建护理任务实体类，并将要更新的数据填充的实体类中
        NursingTask nursingTask = NursingTask.builder()
                .estimatedServerTime(estimatedServerTime)
                .id(taskId)
                .build();
        //执行更新操作，对数据库数据进行更新
        this.updateById(nursingTask);
    }
}
